{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "dzuTm4q7kRPp"
      },
      "outputs": [],
      "source": [
        "from __future__ import print_function\n",
        "import torch"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "2aNLsZ_8kRPr",
        "outputId": "51acb0bc-00e1-48e6-fefb-f547d2ba4a7e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'1.12.1+cu113'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 2
        }
      ],
      "source": [
        "torch.version.__version__"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "iI2dz-tWsx16"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LIuaBsJMkRPs"
      },
      "source": [
        "# PyTorch basics"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IhO2DeXckRPu"
      },
      "source": [
        "The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are\n",
        "defined. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful\n",
        "utilities.\n",
        "It has a CUDA counterpart, that enables you to run your tensor computations on an NVIDIA GPU with compute\n",
        "capability >= 2.0."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "u9m0U33QkRPv"
      },
      "outputs": [],
      "source": [
        "x = [12,23,34,45,56,67,78]"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Scalar\n",
        "scalar = torch.tensor(10)\n",
        "scalar"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3jR9P3xIpdfc",
        "outputId": "c2a296f4-0f7b-4c72-bcdc-4af55673d457"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(10)"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "scalar.ndim"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Z-u8S0bZpdcR",
        "outputId": "0603e52d-5f27-40bf-d073-be9bd34923c7"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "scalar.item()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ngObhZPFqiyg",
        "outputId": "01aaf2e4-b1f2-47cf-ec57-fd45d234f663"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "10"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#vector\n",
        "vector = torch.tensor([5,5])\n",
        "vector"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Cnub9SrBpdYp",
        "outputId": "f090e159-57fd-4ef9-8e6c-567fb72034cb"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([5, 5])"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "vector.ndim"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "V23aZ6OCpdVN",
        "outputId": "730bb128-aa47-454b-ee12-47ddf00ab288"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "vector.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eOapMixJpczW",
        "outputId": "043f84a4-daba-4a40-d4e5-d44d85ab329a"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2])"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Matrix\n",
        "matrix = torch.tensor([[4, 5], \n",
        "                       [10, 110]])\n",
        "matrix"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gD8skzwYqpZT",
        "outputId": "e30c34e0-7702-48f7-d0d1-5b483f56c8ec"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[  4,   5],\n",
              "        [ 10, 110]])"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "matrix.ndim"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wiJo74xPqpH5",
        "outputId": "e5040daa-3ca2-4705-ca38-c07e61c1db3a"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "matrix.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pD7hmcX0qoy3",
        "outputId": "b3ea9613-e4df-4182-9a4a-c1bd1c1479f1"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([2, 2])"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Tensor is multidimensional\n",
        "tensor = torch.tensor([[[4,64, 5,4], \n",
        "                       [10,20,30, 110],\n",
        "                        [45,34,67,40],\n",
        "                        [56,67,89,90]]])\n",
        "tensor"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RAa7U1brrj-v",
        "outputId": "766ef70e-f41d-4c4c-c610-88c11c3ad36c"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[  4,  64,   5,   4],\n",
              "         [ 10,  20,  30, 110],\n",
              "         [ 45,  34,  67,  40],\n",
              "         [ 56,  67,  89,  90]]])"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tensor.ndim"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5nEixTDvrj3V",
        "outputId": "de1065d5-7500-4df4-b7e1-2bd404acd033"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tensor.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2CJMuJf4rjuD",
        "outputId": "e16da783-368f-4864-fead-c647ac4beb2d"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([1, 4, 4])"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tensor.dtype"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YfJK0nNOrjmJ",
        "outputId": "7f946c4d-b908-46e6-bd17-c4c58bad07bd"
      },
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.int64"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tensor.device"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ELC5PGmrrjfJ",
        "outputId": "aa583060-678d-4dcd-e602-3c8d5bc9e3d2"
      },
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "device(type='cpu')"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tensor"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5piwpslAIs42",
        "outputId": "1eea20cb-32e2-439d-e402-5ccf06e4c356"
      },
      "execution_count": 133,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[  4,  64,   5,   4],\n",
              "         [ 10,  20,  30, 110],\n",
              "         [ 45,  34,  67,  40],\n",
              "         [ 56,  67,  89,  90]]])"
            ]
          },
          "metadata": {},
          "execution_count": 133
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Element-wise matrix mutlication\n",
        "tensor * tensor"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EfmzMVjPrjX2",
        "outputId": "62fe7d2c-c79f-4046-d78e-15a38799d9a9"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[   16,  4096,    25,    16],\n",
              "         [  100,   400,   900, 12100],\n",
              "         [ 2025,  1156,  4489,  1600],\n",
              "         [ 3136,  4489,  7921,  8100]]])"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.matmul(tensor, tensor)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XLdeYthSrjP8",
        "outputId": "2912bea1-90ad-438e-d056-b14466153e91"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[ 1105,  1974,  2631,  7616],\n",
              "         [ 7750,  9430, 12450, 13340],\n",
              "         [ 5775,  8518,  9294, 10200],\n",
              "         [ 9939, 13980, 16263, 19254]]])"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tensor @ tensor"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7qr32Y_NrjGy",
        "outputId": "beb58efa-c1c8-4200-b90e-c78b91cc169b"
      },
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[ 1105,  1974,  2631,  7616],\n",
              "         [ 7750,  9430, 12450, 13340],\n",
              "         [ 5775,  8518,  9294, 10200],\n",
              "         [ 9939, 13980, 16263, 19254]]])"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# for reproducing the same sample\n",
        "torch.manual_seed(123)\n",
        "torch.rand(10)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9uDVuwO3ri8e",
        "outputId": "51e896a3-cf80-4c16-85f5-49acf338e480"
      },
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.2961, 0.5166, 0.2517, 0.6886, 0.0740, 0.8665, 0.1366, 0.1025, 0.1841,\n",
              "        0.7264])"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.rand(10)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5qOHxqSiriyR",
        "outputId": "5c11c089-34cd-4bed-d330-1ed3d32d2a57"
      },
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.3153, 0.6871, 0.0756, 0.1966, 0.3164, 0.4017, 0.1186, 0.8274, 0.3821,\n",
              "        0.6605])"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.manual_seed(123)\n",
        "torch.rand(10)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "c19Lr57orihn",
        "outputId": "94087ed0-6c21-4bb6-f95f-7c57708f4dd3"
      },
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.2961, 0.5166, 0.2517, 0.6886, 0.0740, 0.8665, 0.1366, 0.1025, 0.1841,\n",
              "        0.7264])"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2QcJGGB_kRPw",
        "outputId": "3ccaa9a9-abf4-4cc2-a24f-4d9b2409fa96"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ],
      "source": [
        "torch.is_tensor(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fFBm2e7WkRPw",
        "outputId": "bebc8458-ff25-4abd-a09f-3b02e56f7e17"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 5
        }
      ],
      "source": [
        "torch.is_storage(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "UQeYug-8kRPy"
      },
      "outputs": [],
      "source": [
        "y = torch.randn(1,2,3,4,5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CNh2HQi0kRPy",
        "outputId": "01befbb3-0811-4039-db14-fc77d77d600d"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 7
        }
      ],
      "source": [
        "torch.is_tensor(y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pLz0mTAskRPz",
        "outputId": "b8a796d5-94bf-4137-9b10-ca0357507375"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ],
      "source": [
        "torch.is_storage(y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EFY197_-kRP0",
        "outputId": "9727e2f9-b449-4487-88de-414ae3eff062"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "120"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ],
      "source": [
        "torch.numel(y) # the total number of elements in the input Tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aRXXeKP-kRP0",
        "outputId": "1b7ac53a-ab36-4811-8840-7e34c53f144e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ],
      "source": [
        "torch.zeros(4,4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WPQYjHWlkRP1",
        "outputId": "552f6b15-c11a-43b9-d089-08e886931898"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "16"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ],
      "source": [
        "torch.numel(torch.zeros(4,4))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CIZ02WvIkRP1",
        "outputId": "616c5b60-61a8-4227-d677-ef48929cec5b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 0., 0.],\n",
              "        [0., 1., 0.],\n",
              "        [0., 0., 1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ],
      "source": [
        "torch.eye(3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gmdPSTudkRP2",
        "outputId": "cef11ffe-6e69-4089-973c-be342139aca6"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 0., 0., 0., 0.],\n",
              "        [0., 1., 0., 0., 0.],\n",
              "        [0., 0., 1., 0., 0.],\n",
              "        [0., 0., 0., 1., 0.],\n",
              "        [0., 0., 0., 0., 1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ],
      "source": [
        "torch.eye(5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XXGtuxOAkRP2",
        "outputId": "52f55f52-69d3-4fc8-c5b0-da65b4235e24"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 0., 0., 0.],\n",
              "        [0., 1., 0., 0.],\n",
              "        [0., 0., 1., 0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ],
      "source": [
        "torch.eye(3,4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TQ4v_IjDkRP2",
        "outputId": "7674ad4f-e2e2-47e8-8a18-fc678d83d241"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 0., 0., 0.],\n",
              "        [0., 1., 0., 0.],\n",
              "        [0., 0., 1., 0.],\n",
              "        [0., 0., 0., 1.],\n",
              "        [0., 0., 0., 0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ],
      "source": [
        "torch.eye(5,4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9ZaLnJ28kRP3",
        "outputId": "d37e0af2-f164-4ffb-d46f-c0ede6bb531a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "list"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ],
      "source": [
        "type(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SW1l2C-WkRP3"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "x1 = np.array(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "S5J7trg0kRP3",
        "outputId": "a3826fbf-5bbc-4b05-89a8-a5a44dfaa16e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([12, 23, 34, 45, 56, 67, 78])"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ],
      "source": [
        "x1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rXNEtCTBkRP4",
        "outputId": "15c7176c-566b-403f-b8b6-9d91c30201e2"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([12, 23, 34, 45, 56, 67, 78])"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ],
      "source": [
        "torch.from_numpy(x1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ESNgGkGtkRP4",
        "outputId": "bcf235b1-bf82-4926-fa3f-b842a80400f9"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 2.0000,  2.3333,  2.6667,  3.0000,  3.3333,  3.6667,  4.0000,  4.3333,\n",
              "         4.6667,  5.0000,  5.3333,  5.6667,  6.0000,  6.3333,  6.6667,  7.0000,\n",
              "         7.3333,  7.6667,  8.0000,  8.3333,  8.6667,  9.0000,  9.3333,  9.6667,\n",
              "        10.0000])"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ],
      "source": [
        "torch.linspace(2, 10, steps=25) #linear spacing"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "215djyTBkRP4",
        "outputId": "f96d980d-8395-4bb5-dde6-367fefa456b4"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-1.0000e+01, -8.5714e+00, -7.1429e+00, -5.7143e+00, -4.2857e+00,\n",
              "        -2.8571e+00, -1.4286e+00, -2.3842e-07,  1.4286e+00,  2.8571e+00,\n",
              "         4.2857e+00,  5.7143e+00,  7.1429e+00,  8.5714e+00,  1.0000e+01])"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ],
      "source": [
        "torch.linspace(-10, 10, steps=15)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xzEV5qB2kRP4",
        "outputId": "9804cffb-7b54-48e2-e15e-e6a2c6c567d3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1.0000e-10, 2.6827e-09, 7.1969e-08, 1.9307e-06, 5.1795e-05, 1.3895e-03,\n",
              "        3.7276e-02, 1.0000e+00, 2.6827e+01, 7.1969e+02, 1.9307e+04, 5.1795e+05,\n",
              "        1.3895e+07, 3.7276e+08, 1.0000e+10])"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ],
      "source": [
        "torch.logspace(start=-10, end=10, steps=15) #logarithmic spacing"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bYANwau2kRP5",
        "outputId": "4761445b-3202-4e9c-98df-e4aaa3dd60d9"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1., 1., 1., 1.])"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ],
      "source": [
        "torch.ones(4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_v9xxNbUkRP5",
        "outputId": "6a4280de-7b93-4ddb-a282-5ef8a8e43a9b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1., 1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ],
      "source": [
        "torch.ones(4,5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pYUpeUz5kRP5",
        "outputId": "c2b18ad9-d26d-4f00-fe34-c112b14abe0c"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.1408, 0.4445, 0.4251, 0.2663, 0.3743, 0.4784, 0.3760, 0.1876, 0.2151,\n",
              "        0.6876])"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ],
      "source": [
        "# random numbers from a uniform distribution between the values \n",
        "# 0 and 1\n",
        "torch.rand(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Z39NGXXdkRP5",
        "outputId": "4aba4d07-53d4-4a70-9570-24b3d7a50d96"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.2733, 0.0302, 0.8835, 0.9537, 0.9662],\n",
              "        [0.6296, 0.3106, 0.4029, 0.8133, 0.1697],\n",
              "        [0.8578, 0.6517, 0.0440, 0.6197, 0.9889],\n",
              "        [0.8614, 0.6288, 0.2158, 0.4593, 0.2444]])"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ],
      "source": [
        "torch.rand(4, 5) \n",
        "# random values between 0 and 1 and fillied with a matrix of \n",
        "# size rows 4 and columns 5"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "R31k0_EFkRP6",
        "outputId": "4e03ce23-7ae8-4d29-8118-051b95a10d12"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 1.0115, -0.7502,  1.1994,  0.8736,  0.5633, -0.7702,  0.1826, -1.9931,\n",
              "         0.5159,  0.1521])"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ],
      "source": [
        "#random numbers from a normal distribution, \n",
        "#with mean =0 and standard deviation =1\n",
        "torch.randn(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "762wButAkRP6",
        "outputId": "524e0ab9-ab32-4d40-a0e0-5a7a0389ec5b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.3744,  2.1839, -1.8229,  1.0682,  1.5394],\n",
              "        [ 0.9689, -1.3085, -0.3300,  0.3960, -0.6079],\n",
              "        [ 2.3485,  1.2880,  0.6754, -2.0426, -0.3121],\n",
              "        [-0.4897, -1.5335,  0.0467, -0.6213,  1.7185]])"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ],
      "source": [
        "torch.randn(4, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SIiPAP-ckRP6",
        "outputId": "d279deba-09d3-45af-d299-473e9a34e6c3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1, 6, 3, 2, 0, 8, 4, 5, 7, 9])"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ],
      "source": [
        "#selecting values from a range, this is called random permutation\n",
        "torch.randperm(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TjatDKnykRP6",
        "outputId": "a9231718-bb8b-442a-fab2-a60480047298"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38])"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ],
      "source": [
        "#usage of range function \n",
        "torch.arange(10, 40,2) #step size 2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ndYb8Kx3kRP7",
        "outputId": "722da66b-524e-4e8a-fa2e-5aa4294511c6"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,\n",
              "        28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39])"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ],
      "source": [
        "torch.arange(10,40) #step size 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_AiSwzYqkRP7",
        "outputId": "75d3827e-fca4-44f0-a7e8-175c45ac7982"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.0085, -0.8545, -0.6958,  1.6716, -0.0118],\n",
              "        [ 0.2134,  1.1154, -0.6426, -1.3651, -1.5724],\n",
              "        [ 0.2452,  0.8356,  2.0297, -0.2397,  0.8560],\n",
              "        [ 0.9786, -0.8538, -0.6449,  0.3903,  1.5966]])"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ],
      "source": [
        "d = torch.randn(4, 5)\n",
        "d"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6oSoZBB2kRP7",
        "outputId": "fac1485f-63e7-4a49-be94-baa5e344e8e9"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([1, 4, 3, 1])"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ],
      "source": [
        "torch.argmin(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dik7vt0jkRP7",
        "outputId": "47935c07-fff4-450a-9975-379c1ad25873"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([3, 1, 2, 4])"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ],
      "source": [
        "torch.argmax(d,dim=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ukG9cgUHkRP8",
        "outputId": "950aa853-0745-44ee-f4a7-365fac26901e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ],
      "source": [
        "# create a 2dtensor filled with values as 0\n",
        "torch.zeros(4,5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ejd0D8VskRP8",
        "outputId": "a9a14882-0e80-4c43-80c2-9d580415570d"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ],
      "source": [
        "# create a 1d tensor filled with values as 0\n",
        "torch.zeros(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GQcvBGs-kRP8"
      },
      "outputs": [],
      "source": [
        "#indexing and performing operation on the tensors\n",
        "x = torch.randn(4,5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f6id6TqHkRP8",
        "outputId": "b1ef423c-8e74-4d50-d116-f232a4f16fe5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BRNWkcRwkRP8",
        "outputId": "d0f8c661-8f61-46c6-da4f-a50815078c03"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280],\n",
              "        [-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 39
        }
      ],
      "source": [
        "#concatenate two tensors\n",
        "torch.cat((x,x))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pypEQTVDkRP8",
        "outputId": "b83266ee-b984-4086-a85f-878aa3c2d7ef"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280],\n",
              "        [-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280],\n",
              "        [-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ],
      "source": [
        "#concatenate n times based on array size\n",
        "torch.cat((x,x,x))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "n6m9U6c8kRP9",
        "outputId": "77b43f76-beab-4584-df79-f8f73f3ff8d0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114, -1.5343, -1.3533, -0.8621,\n",
              "         -1.1674, -0.1114, -1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379,  0.2790,  0.0463,  1.5364,\n",
              "         -0.1287,  0.6379,  0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602, -0.4542,  0.5196,  0.2335,\n",
              "         -0.5135, -0.6602, -0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280, -0.6930,  0.0541, -0.8463,\n",
              "         -0.4498, -0.0280, -0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ],
      "source": [
        "#concatenate n times based on array size, over column\n",
        "torch.cat((x,x,x),1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2CCJG5cBkRP9",
        "outputId": "9b2b3355-9974-4768-d64f-6aad4b5a3de8"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280],\n",
              "        [-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 42
        }
      ],
      "source": [
        "#concatenate n times based on array size, over rows\n",
        "torch.cat((x,x),0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "jQ1WQ-TQkRP9"
      },
      "outputs": [],
      "source": [
        "#how to split a tensor among small chunks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "o_zI5lR2kRP9",
        "outputId": "21c941b9-dcd3-47bf-f211-35d40c076d97"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Help on built-in function chunk in module torch:\n",
            "\n",
            "chunk(...)\n",
            "    chunk(input, chunks, dim=0) -> List of Tensors\n",
            "    \n",
            "    Attempts to split a tensor into the specified number of chunks. Each chunk is a view of\n",
            "    the input tensor.\n",
            "    \n",
            "    \n",
            "    .. note::\n",
            "    \n",
            "        This function may return less then the specified number of chunks!\n",
            "    \n",
            "    .. seealso::\n",
            "    \n",
            "        :func:`torch.tensor_split` a function that always returns exactly the specified number of chunks\n",
            "    \n",
            "    If the tensor size along the given dimesion :attr:`dim` is divisible by :attr:`chunks`,\n",
            "    all returned chunks will be the same size.\n",
            "    If the tensor size along the given dimension :attr:`dim` is not divisible by :attr:`chunks`,\n",
            "    all returned chunks will be the same size, except the last one.\n",
            "    If such division is not possible, this function may return less\n",
            "    than the specified number of chunks.\n",
            "    \n",
            "    Arguments:\n",
            "        input (Tensor): the tensor to split\n",
            "        chunks (int): number of chunks to return\n",
            "        dim (int): dimension along which to split the tensor\n",
            "    \n",
            "    Example::\n",
            "        >>> torch.arange(11).chunk(6)\n",
            "        (tensor([0, 1]),\n",
            "         tensor([2, 3]),\n",
            "         tensor([4, 5]),\n",
            "         tensor([6, 7]),\n",
            "         tensor([8, 9]),\n",
            "         tensor([10]))\n",
            "        >>> torch.arange(12).chunk(6)\n",
            "        (tensor([0, 1]),\n",
            "         tensor([2, 3]),\n",
            "         tensor([4, 5]),\n",
            "         tensor([6, 7]),\n",
            "         tensor([8, 9]),\n",
            "         tensor([10, 11]))\n",
            "        >>> torch.arange(13).chunk(6)\n",
            "        (tensor([0, 1, 2]),\n",
            "         tensor([3, 4, 5]),\n",
            "         tensor([6, 7, 8]),\n",
            "         tensor([ 9, 10, 11]),\n",
            "         tensor([12]))\n",
            "\n"
          ]
        }
      ],
      "source": [
        "help(torch.chunk)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hsj7msbukRP-",
        "outputId": "5ef6fe7c-a8dd-49d0-8731-55bd090b1c56"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "tensor([[-0.5899, -1.3432, -1.0576, -0.1696],\n",
            "        [ 0.2623, -0.1585,  1.0178, -0.2216],\n",
            "        [-1.1716, -1.2771,  0.8073, -0.7717],\n",
            "        [ 0.1768,  0.6423, -0.3200, -0.0480]])\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([[-0.5899, -1.3432, -1.0576, -0.1696],\n",
              "         [ 0.2623, -0.1585,  1.0178, -0.2216]]),\n",
              " tensor([[-1.1716, -1.2771,  0.8073, -0.7717],\n",
              "         [ 0.1768,  0.6423, -0.3200, -0.0480]]))"
            ]
          },
          "metadata": {},
          "execution_count": 45
        }
      ],
      "source": [
        "a = torch.randn(4, 4)\n",
        "print(a)\n",
        "torch.chunk(a,2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JdmM6zAMkRP-",
        "outputId": "a58fc748-b1a6-4708-c5cc-56fe8c05aea0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([[-0.5899, -1.3432, -1.0576, -0.1696],\n",
              "         [ 0.2623, -0.1585,  1.0178, -0.2216]]),\n",
              " tensor([[-1.1716, -1.2771,  0.8073, -0.7717],\n",
              "         [ 0.1768,  0.6423, -0.3200, -0.0480]]))"
            ]
          },
          "metadata": {},
          "execution_count": 46
        }
      ],
      "source": [
        "torch.chunk(a,2,0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HFUA-WVzkRP-",
        "outputId": "d92df1a2-a254-45ec-a019-24293c73daa4"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([[-0.5899, -1.3432],\n",
              "         [ 0.2623, -0.1585],\n",
              "         [-1.1716, -1.2771],\n",
              "         [ 0.1768,  0.6423]]), tensor([[-1.0576, -0.1696],\n",
              "         [ 1.0178, -0.2216],\n",
              "         [ 0.8073, -0.7717],\n",
              "         [-0.3200, -0.0480]]))"
            ]
          },
          "metadata": {},
          "execution_count": 47
        }
      ],
      "source": [
        "torch.chunk(a,2,1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JiedAr_SkRP-",
        "outputId": "617f270d-03b0-4e6f-e3c8-291a979c1035"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[11., 12.],\n",
              "        [23., 24.]])"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ],
      "source": [
        "torch.Tensor([[11,12],[23,24]])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Xt_2FSAikRP-",
        "outputId": "44b48038-5cfe-481b-923f-76d2723e9c3d"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[11., 11.],\n",
              "        [24., 23.]])"
            ]
          },
          "metadata": {},
          "execution_count": 49
        }
      ],
      "source": [
        "torch.gather(torch.Tensor([[11,12],[23,24]]), 1, \n",
        "             torch.LongTensor([[0,0],[1,0]]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4zdUzvavkRP_",
        "outputId": "16096cf6-2d04-4124-dd3b-bf6f537486c2"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 0],\n",
              "        [1, 0]])"
            ]
          },
          "metadata": {},
          "execution_count": 50
        }
      ],
      "source": [
        " torch.LongTensor([[0,0],[1,0]]) \n",
        "#the 1D tensor containing the indices to index"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QfP7TKt4kRP_",
        "outputId": "dd78a644-6442-4ef7-ff15-2d3fe0b2bcca"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "tensor([[-0.9183, -2.3470,  1.5208, -0.1585],\n",
            "        [-0.6741, -0.6297,  0.2581, -1.1954],\n",
            "        [ 1.0443, -1.3408,  0.7863, -0.6056],\n",
            "        [-0.6946, -0.5963,  0.1936, -2.0625]])\n"
          ]
        }
      ],
      "source": [
        "a = torch.randn(4, 4)\n",
        "print(a)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AzzT04G1kRP_"
      },
      "outputs": [],
      "source": [
        "indices = torch.LongTensor([0, 2])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XimTaudXkRP_",
        "outputId": "c1b27208-737b-4a8e-d8de-1bca4ce386aa"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.9183, -2.3470,  1.5208, -0.1585],\n",
              "        [ 1.0443, -1.3408,  0.7863, -0.6056]])"
            ]
          },
          "metadata": {},
          "execution_count": 53
        }
      ],
      "source": [
        "torch.index_select(a, 0, indices)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hV2--K7QkRP_",
        "outputId": "5ce0023c-f7f7-4fee-f4d9-75f4aaa417ec"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.9183,  1.5208],\n",
              "        [-0.6741,  0.2581],\n",
              "        [ 1.0443,  0.7863],\n",
              "        [-0.6946,  0.1936]])"
            ]
          },
          "metadata": {},
          "execution_count": 54
        }
      ],
      "source": [
        "torch.index_select(a, 1, indices)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OOAP18uIkRP_",
        "outputId": "9c66b0e7-8ae0-4096-ac78-f3b45af7db2f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0],\n",
              "        [2]])"
            ]
          },
          "metadata": {},
          "execution_count": 55
        }
      ],
      "source": [
        "#identify null input tensors using nonzero function\n",
        "torch.nonzero(torch.tensor([10,00,23,0,0.0]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BgsjXabfkRQA",
        "outputId": "ed94b630-c05c-472d-b166-a82f04282e0f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0],\n",
              "        [2]])"
            ]
          },
          "metadata": {},
          "execution_count": 56
        }
      ],
      "source": [
        "torch.nonzero(torch.Tensor([10,00,23,0,0.0]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tEY1SHtNkRQA",
        "outputId": "14929f38-ccfc-4c4b-82bc-ee1e5e3b3a14"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([12, 21]), tensor([34, 32]), tensor([45, 54]), tensor([56, 65]))"
            ]
          },
          "metadata": {},
          "execution_count": 57
        }
      ],
      "source": [
        "# splitting the tensor into small chunks\n",
        "torch.split(torch.tensor([12,21,34,32,45,54,56,65]),2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Em2Wxar_kRQA",
        "outputId": "5038602d-fcd8-4347-acac-c531b76cd921"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([12, 21, 34]), tensor([32, 45, 54]), tensor([56, 65]))"
            ]
          },
          "metadata": {},
          "execution_count": 58
        }
      ],
      "source": [
        "# splitting the tensor into small chunks\n",
        "torch.split(torch.tensor([12,21,34,32,45,54,56,65]),3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1fZecHa4kRQA",
        "outputId": "1b54a23b-16e7-4599-dc05-6f4517b13989"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.]],\n",
              "\n",
              "        [[0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.]],\n",
              "\n",
              "        [[0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.]]])"
            ]
          },
          "metadata": {},
          "execution_count": 59
        }
      ],
      "source": [
        "torch.zeros(3,2,4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4q1oMmK2kRQB",
        "outputId": "cb8bf129-6324-4f91-d49c-c6f4c8f4de71"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([3, 2, 4])"
            ]
          },
          "metadata": {},
          "execution_count": 60
        }
      ],
      "source": [
        "torch.zeros(3,2,4).size()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "LBPUC-chkRQB"
      },
      "outputs": [],
      "source": [
        "#how to reshape the tensors along a new dimension"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yga5qsljkRQB",
        "outputId": "4bcc553b-df75-45ba-8d03-0c061281e6f3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 62
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "24oCAEgCkRQB",
        "outputId": "04a1be58-80ec-48f7-902c-0698492bba02"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343,  0.2790, -0.4542, -0.6930],\n",
              "        [-1.3533,  0.0463,  0.5196,  0.0541],\n",
              "        [-0.8621,  1.5364,  0.2335, -0.8463],\n",
              "        [-1.1674, -0.1287, -0.5135, -0.4498],\n",
              "        [-0.1114,  0.6379, -0.6602, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 63
        }
      ],
      "source": [
        "x.t() #transpose is one option to change the shape of the tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "1gtAAafBkRQB"
      },
      "outputs": [],
      "source": [
        "# transpose partially based on rows and columns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pPWlIUAikRQB",
        "outputId": "5e27bad7-531d-456b-f787-3f4430a54fbe"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343,  0.2790, -0.4542, -0.6930],\n",
              "        [-1.3533,  0.0463,  0.5196,  0.0541],\n",
              "        [-0.8621,  1.5364,  0.2335, -0.8463],\n",
              "        [-1.1674, -0.1287, -0.5135, -0.4498],\n",
              "        [-0.1114,  0.6379, -0.6602, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 65
        }
      ],
      "source": [
        "x.transpose(1,0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "7x1lHezGkRQB"
      },
      "outputs": [],
      "source": [
        "# how to remove a dimension from a tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mbTx9nYukRQC",
        "outputId": "23ad69c8-26c0-43db-a263-b4abb13dd035"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 67
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mRi5sJUOkRQC",
        "outputId": "abf683e6-213f-4fca-ad1c-3b3897049d81"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([-1.5343,  0.2790, -0.4542, -0.6930]),\n",
              " tensor([-1.3533,  0.0463,  0.5196,  0.0541]),\n",
              " tensor([-0.8621,  1.5364,  0.2335, -0.8463]),\n",
              " tensor([-1.1674, -0.1287, -0.5135, -0.4498]),\n",
              " tensor([-0.1114,  0.6379, -0.6602, -0.0280]))"
            ]
          },
          "metadata": {},
          "execution_count": 68
        }
      ],
      "source": [
        "torch.unbind(x,1) #dim=1 removing a column"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "n2JPC1GBkRQC",
        "outputId": "8b469c57-804b-4d1c-fe16-9326bc4c4f2b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor([-1.5343, -1.3533, -0.8621, -1.1674, -0.1114]),\n",
              " tensor([ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379]),\n",
              " tensor([-0.4542,  0.5196,  0.2335, -0.5135, -0.6602]),\n",
              " tensor([-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]))"
            ]
          },
          "metadata": {},
          "execution_count": 69
        }
      ],
      "source": [
        "torch.unbind(x) #dim=0 removing a row"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "phy4cr43kRQC",
        "outputId": "e4c54b24-a886-4c3c-f854-28c027fddf41"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 70
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "2yxUcKzpkRQC"
      },
      "outputs": [],
      "source": [
        "#how to compute the basic mathematrical functions"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7IiwZ7PmkRQD",
        "outputId": "5d03b4ac-181b-4e93-b6ad-77e38c9254c3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10., 23.,  3.])"
            ]
          },
          "metadata": {},
          "execution_count": 72
        }
      ],
      "source": [
        "torch.abs(torch.FloatTensor([-10, -23, 3.000]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cU9XcaeEkRQD",
        "outputId": "919766c6-a1bd-456d-e2f4-bc2b243d63fd"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[18.4657, 18.6467, 19.1379, 18.8326, 19.8886],\n",
              "        [20.2790, 20.0463, 21.5364, 19.8713, 20.6379],\n",
              "        [19.5458, 20.5196, 20.2335, 19.4865, 19.3398],\n",
              "        [19.3070, 20.0541, 19.1537, 19.5502, 19.9720]])"
            ]
          },
          "metadata": {},
          "execution_count": 73
        }
      ],
      "source": [
        "#adding value to the existing tensor, scalar addition\n",
        "torch.add(x,20)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "62GKbMb0kRQD",
        "outputId": "396924da-248b-48ec-ce97-5b3b0f828b02"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 74
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HAJhMhlJkRQD",
        "outputId": "fc16dbd7-b5bb-4806-a44c-ca03fd491965"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-3.0686, -2.7065, -1.7242, -2.3349, -0.2227],\n",
              "        [ 0.5581,  0.0926,  3.0727, -0.2575,  1.2757],\n",
              "        [-0.9084,  1.0392,  0.4670, -1.0270, -1.3203],\n",
              "        [-1.3859,  0.1082, -1.6926, -0.8995, -0.0560]])"
            ]
          },
          "metadata": {},
          "execution_count": 75
        }
      ],
      "source": [
        "# scalar multiplication\n",
        "torch.mul(x,2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pBPm4dbykRQE",
        "outputId": "ca5696d0-56ea-4649-f51c-fb3d7cb81ccf"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5343, -1.3533, -0.8621, -1.1674, -0.1114],\n",
              "        [ 0.2790,  0.0463,  1.5364, -0.1287,  0.6379],\n",
              "        [-0.4542,  0.5196,  0.2335, -0.5135, -0.6602],\n",
              "        [-0.6930,  0.0541, -0.8463, -0.4498, -0.0280]])"
            ]
          },
          "metadata": {},
          "execution_count": 76
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "O0ZWzQVCkRQE"
      },
      "outputs": [],
      "source": [
        "# how do we represent the equation in the form of a tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "tusgZjdikRQE"
      },
      "outputs": [],
      "source": [
        "# y = intercept + (beta * x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2nSEHkaykRQE",
        "outputId": "92b82253-2f68-4ddd-e166-b2d39b99b524"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-1.1444])"
            ]
          },
          "metadata": {},
          "execution_count": 79
        }
      ],
      "source": [
        "intercept = torch.randn(1)\n",
        "intercept"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4QnQf3t2kRQE",
        "outputId": "f0ee3f28-04b4-4c12-fc0f-f34df24d269b"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.3517, -0.3991],\n",
              "        [-0.4170, -0.1862]])"
            ]
          },
          "metadata": {},
          "execution_count": 80
        }
      ],
      "source": [
        "x = torch.randn(2, 2)\n",
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "w_erWmcbkRQF",
        "outputId": "d7b9fb06-760a-40b7-8b61-424942da5740"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.7456"
            ]
          },
          "metadata": {},
          "execution_count": 81
        }
      ],
      "source": [
        "beta = 0.7456\n",
        "beta"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "350gBEMHkRQF",
        "outputId": "e64dc360-018b-4502-a446-30b698b0dc59"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.0078, -0.2976],\n",
              "        [-0.3109, -0.1388]])"
            ]
          },
          "metadata": {},
          "execution_count": 82
        }
      ],
      "source": [
        "torch.mul(x,beta)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "teEsDDDgkRQF",
        "outputId": "4b17bc94-9874-4460-d632-f712cbf6fa02"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: UserWarning: This overload of add is deprecated:\n",
            "\tadd(Tensor input, Number alpha, Tensor other, *, Tensor out)\n",
            "Consider using one of the following signatures instead:\n",
            "\tadd(Tensor input, Tensor other, *, Number alpha, Tensor out) (Triggered internally at  ../torch/csrc/utils/python_arg_parser.cpp:1174.)\n",
            "  \"\"\"Entry point for launching an IPython kernel.\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.4984, -1.2524],\n",
              "        [-1.2703, -1.0395]])"
            ]
          },
          "metadata": {},
          "execution_count": 83
        }
      ],
      "source": [
        "torch.add(x,beta,intercept)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sF3EH7FQkRQF",
        "outputId": "131bcad3-53ed-4aef-93e1-f54a6627b0a7"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.5469,  0.4568],\n",
              "        [ 0.4773,  0.2131]])"
            ]
          },
          "metadata": {},
          "execution_count": 84
        }
      ],
      "source": [
        "torch.mul(intercept,x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "k-GEI_R_kRQG",
        "outputId": "a647b35e-5247-4b82-ddf8-0fac28d1860f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.0078, -0.2976],\n",
              "        [-0.3109, -0.1388]])"
            ]
          },
          "metadata": {},
          "execution_count": 85
        }
      ],
      "source": [
        "torch.mul(x,beta)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Nk-dntIAkRQG",
        "outputId": "c1eb8492-08a4-4124-8df0-5e80e98179d5"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.5391,  0.1592],\n",
              "        [ 0.1663,  0.0743]])"
            ]
          },
          "metadata": {},
          "execution_count": 86
        }
      ],
      "source": [
        "## y = intercept + (beta * x)\n",
        "torch.add(torch.mul(intercept,x),torch.mul(x,beta)) # tensor y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TZUKuEBukRQG",
        "outputId": "1a9c7f03-d96a-401d-c674-fabc42b3180d"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.1117, -0.4966,  0.1631, -0.8817,  0.0539],\n",
              "        [ 0.6684, -0.0597, -0.4675, -0.2153, -0.7141],\n",
              "        [-1.0831, -0.5547,  0.9717, -0.5150,  1.4255],\n",
              "        [ 0.7987, -1.4949,  1.4778, -0.1696, -0.9919],\n",
              "        [-1.4569,  0.2563, -0.4030,  0.4195,  0.9380]])"
            ]
          },
          "metadata": {},
          "execution_count": 87
        }
      ],
      "source": [
        "# how to round up tensor values\n",
        "torch.manual_seed(1234)\n",
        "torch.randn(5,5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zOTKT-vQkRQG",
        "outputId": "657d6bc8-e14f-43db-8e63-669009e14035"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0., -0.,  1., -0.,  1.],\n",
              "        [ 1., -0., -0., -0., -0.],\n",
              "        [-1., -0.,  1., -0.,  2.],\n",
              "        [ 1., -1.,  2., -0., -0.],\n",
              "        [-1.,  1., -0.,  1.,  1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 88
        }
      ],
      "source": [
        "torch.manual_seed(1234)\n",
        "torch.ceil(torch.randn(5,5))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Zs_mC8hgkRQG",
        "outputId": "7f68d3dc-fee7-4e84-fd43-63f804e853d9"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1., -1.,  0., -1.,  0.],\n",
              "        [ 0., -1., -1., -1., -1.],\n",
              "        [-2., -1.,  0., -1.,  1.],\n",
              "        [ 0., -2.,  1., -1., -1.],\n",
              "        [-2.,  0., -1.,  0.,  0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 89
        }
      ],
      "source": [
        "torch.manual_seed(1234)\n",
        "torch.floor(torch.randn(5,5))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8N4GADCAkRQH",
        "outputId": "61db33f9-68ec-4f34-f8dd-6dc22a2aae2c"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.3000, -0.3000,  0.0000, -0.3000,  0.0000],\n",
              "        [ 0.0000, -0.3000, -0.3000, -0.3000, -0.3000],\n",
              "        [-0.3000, -0.3000,  0.0000, -0.3000,  0.4000],\n",
              "        [ 0.0000, -0.3000,  0.4000, -0.3000, -0.3000],\n",
              "        [-0.3000,  0.0000, -0.3000,  0.0000,  0.0000]])"
            ]
          },
          "metadata": {},
          "execution_count": 90
        }
      ],
      "source": [
        "# truncate the values in a range say 0,1\n",
        "torch.manual_seed(1234)\n",
        "torch.clamp(torch.floor(torch.randn(5,5)), min=-0.3, max=0.4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uCpdg3_2kRQH",
        "outputId": "e24926a7-a293-4875-cec0-56202bf0a5ce"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.3000, -0.3000,  0.0000, -0.3000,  0.0000],\n",
              "        [ 0.0000, -0.3000, -0.3000, -0.3000, -0.3000],\n",
              "        [-0.3000, -0.3000,  0.0000, -0.3000,  1.0000],\n",
              "        [ 0.0000, -0.3000,  1.0000, -0.3000, -0.3000],\n",
              "        [-0.3000,  0.0000, -0.3000,  0.0000,  0.0000]])"
            ]
          },
          "metadata": {},
          "execution_count": 91
        }
      ],
      "source": [
        "#truncate with only lower limit\n",
        "torch.manual_seed(1234)\n",
        "torch.clamp(torch.floor(torch.randn(5,5)), min=-0.3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EMCAYBa6kRQH",
        "outputId": "d0f13ac7-2118-495a-9f8d-2ae8bccc42c4"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-1.0000, -1.0000,  0.0000, -1.0000,  0.0000],\n",
              "        [ 0.0000, -1.0000, -1.0000, -1.0000, -1.0000],\n",
              "        [-2.0000, -1.0000,  0.0000, -1.0000,  0.3000],\n",
              "        [ 0.0000, -2.0000,  0.3000, -1.0000, -1.0000],\n",
              "        [-2.0000,  0.0000, -1.0000,  0.0000,  0.0000]])"
            ]
          },
          "metadata": {},
          "execution_count": 92
        }
      ],
      "source": [
        "#truncate with only upper limit\n",
        "torch.manual_seed(1234)\n",
        "torch.clamp(torch.floor(torch.randn(5,5)), max=0.3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XOFAiEcDkRQH",
        "outputId": "2d40d997-8417-4fb4-f79e-e7515628623f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[13.5168, -3.9914],\n",
              "        [-4.1705, -1.8621]])"
            ]
          },
          "metadata": {},
          "execution_count": 93
        }
      ],
      "source": [
        "#scalar division\n",
        "torch.div(x,0.10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0f5RJVYHkRQH",
        "outputId": "d3e6be62-58f3-4390-8ec5-94d82c804810"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[3.8639, 0.6709],\n",
              "        [0.6590, 0.8301]])"
            ]
          },
          "metadata": {},
          "execution_count": 94
        }
      ],
      "source": [
        "#compute the exponential of a tensor\n",
        "torch.exp(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "D8HXRNJSkRQH",
        "outputId": "e849fac2-8a32-4b44-8016-272f9fc4672f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[3.8639, 0.6709],\n",
              "        [0.6590, 0.8301]])"
            ]
          },
          "metadata": {},
          "execution_count": 95
        }
      ],
      "source": [
        "np.exp(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "vHXyPXaxkRQI"
      },
      "outputs": [],
      "source": [
        "#how to get the fractional portion of each tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Lyxs2IZ6kRQI",
        "outputId": "63f73111-8246-4453-a398-767c49edaf59"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[11.3517,  9.6009],\n",
              "        [ 9.5830,  9.8138]])"
            ]
          },
          "metadata": {},
          "execution_count": 97
        }
      ],
      "source": [
        "torch.add(x,10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QW4prpiokRQI",
        "outputId": "2c070e85-c2dd-400e-be9c-041b94278e14"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.3517, 0.6009],\n",
              "        [0.5830, 0.8138]])"
            ]
          },
          "metadata": {},
          "execution_count": 98
        }
      ],
      "source": [
        "torch.frac(torch.add(x,10))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "l0dd3hW5kRQI"
      },
      "outputs": [],
      "source": [
        "# compute the log of the values in a tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5OWNcVC0kRQI",
        "outputId": "4d24a34f-e9bc-4288-8522-f13003d47bc3"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.3517, -0.3991],\n",
              "        [-0.4170, -0.1862]])"
            ]
          },
          "metadata": {},
          "execution_count": 100
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "KTZVZTIfkRQI",
        "outputId": "f380b76f-411d-42b3-8165-7f9e0c2b7823"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.3013,    nan],\n",
              "        [   nan,    nan]])"
            ]
          },
          "metadata": {},
          "execution_count": 101
        }
      ],
      "source": [
        "torch.log(x) #log of negatives are nan"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EMOiJMPbkRQJ",
        "outputId": "f8d7dc74-1d31-45ea-bd3e-c43e37dad8f0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1.8270, 0.1593],\n",
              "        [0.1739, 0.0347]])"
            ]
          },
          "metadata": {},
          "execution_count": 102
        }
      ],
      "source": [
        "# to rectify the negative values do a power tranforamtion\n",
        "torch.pow(x,2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "xOWR_AlvkRQJ"
      },
      "outputs": [],
      "source": [
        "# rounding up similar to numpy"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8bEXLaqZkRQJ",
        "outputId": "595957f5-b8de-493f-843d-c6d8d7a313e0"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.3517, -0.3991],\n",
              "        [-0.4170, -0.1862]])"
            ]
          },
          "metadata": {},
          "execution_count": 104
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0d9nZ_IdkRQJ",
        "outputId": "ec9d20c5-91fe-40d6-c5f7-e4c3b6b41cb9"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., -0.],\n",
              "        [-0., -0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 105
        }
      ],
      "source": [
        "np.round(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-F_Ge5XVkRQJ",
        "outputId": "935b6e75-6eed-453a-bace-d2b5db415f26"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., -0.],\n",
              "        [-0., -0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 106
        }
      ],
      "source": [
        "torch.round(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "jicTaYVEkRQJ"
      },
      "outputs": [],
      "source": [
        "# how to compute the sigmoid of the input tensor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "S-d5HC-tkRQK",
        "outputId": "1d849043-525c-4eb0-ea53-2f0b11ff491c"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.3517, -0.3991],\n",
              "        [-0.4170, -0.1862]])"
            ]
          },
          "metadata": {},
          "execution_count": 108
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZAWO8zwXkRQK",
        "outputId": "99da928b-b6ed-4561-f0da-528e49c1bb82"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.7944, 0.4015],\n",
              "        [0.3972, 0.4536]])"
            ]
          },
          "metadata": {},
          "execution_count": 109
        }
      ],
      "source": [
        "torch.sigmoid(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "Vs6CBXhekRQK"
      },
      "outputs": [],
      "source": [
        "# finding the square root of the values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "B1pUY6o8kRQK",
        "outputId": "ab39747c-fc6c-40a4-9b90-7dc1560e2214"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.3517, -0.3991],\n",
              "        [-0.4170, -0.1862]])"
            ]
          },
          "metadata": {},
          "execution_count": 111
        }
      ],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4mQadf5xkRQK",
        "outputId": "3477298d-cabc-4424-e9c0-d5242ed2f053"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1.1626,    nan],\n",
              "        [   nan,    nan]])"
            ]
          },
          "metadata": {},
          "execution_count": 112
        }
      ],
      "source": [
        "torch.sqrt(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 37,
      "metadata": {
        "id": "GERdcvYckRQK",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "fda7297a-8210-4aa5-8ae8-49dd598d1a02"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([  10,  160,  310,  460,  610,  760,  910, 1060, 1210, 1360, 1510, 1660,\n",
              "        1810, 1960, 2110, 2260, 2410, 2560, 2710, 2860, 3010, 3160, 3310, 3460,\n",
              "        3610, 3760, 3910, 4060, 4210, 4360, 4510, 4660, 4810, 4960, 5110, 5260,\n",
              "        5410, 5560, 5710, 5860, 6010, 6160, 6310, 6460, 6610, 6760, 6910, 7060,\n",
              "        7210, 7360, 7510, 7660, 7810, 7960, 8110, 8260, 8410, 8560, 8710, 8860,\n",
              "        9010, 9160, 9310, 9460, 9610, 9760, 9910])"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ],
      "source": [
        "# Create a tensor\n",
        "x = torch.arange(10, 10000, 150)\n",
        "x"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(f\"Minimum: {x.min()}\")\n",
        "print(f\"Maximum: {x.max()}\")\n",
        "# print(f\"Mean: {x.mean()}\") # this will error\n",
        "print(f\"Mean: {x.type(torch.float32).mean()}\") # won't work without float datatype\n",
        "print(f\"Sum: {x.sum()}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FLf8W8nauj5A",
        "outputId": "daaa019f-cf68-4b76-de05-2e48911d5564"
      },
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Minimum: 10\n",
            "Maximum: 9910\n",
            "Mean: 4960.0\n",
            "Sum: 332320\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.argmax(x),torch.argmin(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b96-eRgtuszB",
        "outputId": "d998785f-2d50-4926-8c17-4e1c89d1e505"
      },
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor(66), tensor(0))"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.max(x),torch.min(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "S6HdcW6qu8Qp",
        "outputId": "465a2056-92a4-4216-f176-910e1babcedc"
      },
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(tensor(9910), tensor(10))"
            ]
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# how to change data type\n",
        "y = torch.tensor([[39,339.63],\n",
        "[36,667.20],\n",
        "[33,978.07],\n",
        "[31,897.13],\n",
        "[29,178.19],\n",
        "[26,442.25],\n",
        "[24,314.22],\n",
        "[21,547.88],\n",
        "[18,764.25],\n",
        "[16,588.23],\n",
        "[13,773.61]],dtype=torch.float32)"
      ],
      "metadata": {
        "id": "qWpNIUrkvNhu"
      },
      "execution_count": 61,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "y.dtype "
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_uRQJ5r4vbUH",
        "outputId": "e1a93a25-5141-452a-a923-59cfe0c91a41"
      },
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.float32"
            ]
          },
          "metadata": {},
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a float16 tensor\n",
        "tensor_float16 = y.type(torch.float16)\n",
        "tensor_float16"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "H2EHHw6CxHkW",
        "outputId": "56a65cae-b9ec-4204-eb3e-7305d43ae25f"
      },
      "execution_count": 64,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 39.0000, 339.7500],\n",
              "        [ 36.0000, 667.0000],\n",
              "        [ 33.0000, 978.0000],\n",
              "        [ 31.0000, 897.0000],\n",
              "        [ 29.0000, 178.2500],\n",
              "        [ 26.0000, 442.2500],\n",
              "        [ 24.0000, 314.2500],\n",
              "        [ 21.0000, 548.0000],\n",
              "        [ 18.0000, 764.0000],\n",
              "        [ 16.0000, 588.0000],\n",
              "        [ 13.0000, 773.5000]], dtype=torch.float16)"
            ]
          },
          "metadata": {},
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qZriWswexfJW",
        "outputId": "82a16671-b5c8-4bb2-f4c8-c6ff4a348f0c"
      },
      "execution_count": 59,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 39.0000, 339.6300],\n",
              "        [ 36.0000, 667.2000],\n",
              "        [ 33.0000, 978.0700],\n",
              "        [ 31.0000, 897.1300],\n",
              "        [ 29.0000, 178.1900],\n",
              "        [ 26.0000, 442.2500],\n",
              "        [ 24.0000, 314.2200],\n",
              "        [ 21.0000, 547.8800],\n",
              "        [ 18.0000, 764.2500],\n",
              "        [ 16.0000, 588.2300],\n",
              "        [ 13.0000, 773.6100]])"
            ]
          },
          "metadata": {},
          "execution_count": 59
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a int8 tensor\n",
        "tensor_int8 = y.type(torch.int8)\n",
        "tensor_int8\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7WUku7e_xtBv",
        "outputId": "609cf08b-c3a8-4fb4-85ed-a72d2e4d099d"
      },
      "execution_count": 65,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[  39,   83],\n",
              "        [  36, -101],\n",
              "        [  33,  -46],\n",
              "        [  31, -127],\n",
              "        [  29,  -78],\n",
              "        [  26,  -70],\n",
              "        [  24,   58],\n",
              "        [  21,   35],\n",
              "        [  18,   -4],\n",
              "        [  16,   76],\n",
              "        [  13,    5]], dtype=torch.int8)"
            ]
          },
          "metadata": {},
          "execution_count": 65
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Change view (keeps same data as original but changes view)\n",
        "y.view(2,11)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Nfdv4rgryRjr",
        "outputId": "1fbc7183-0410-41b3-9413-2969f877e271"
      },
      "execution_count": 68,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 39.0000, 339.6300,  36.0000, 667.2000,  33.0000, 978.0700,  31.0000,\n",
              "         897.1300,  29.0000, 178.1900,  26.0000],\n",
              "        [442.2500,  24.0000, 314.2200,  21.0000, 547.8800,  18.0000, 764.2500,\n",
              "          16.0000, 588.2300,  13.0000, 773.6100]])"
            ]
          },
          "metadata": {},
          "execution_count": 68
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# stacking and unstacking of tensors\n",
        "A = torch.arange(10,50,5)\n",
        "B = torch.arange(20,60,5)"
      ],
      "metadata": {
        "id": "s42G_AZAy5Gk"
      },
      "execution_count": 96,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "A"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "y-VeJWfC0nnQ",
        "outputId": "7ff565a3-00b1-472e-d089-fae8863ee099"
      },
      "execution_count": 97,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10, 15, 20, 25, 30, 35, 40, 45])"
            ]
          },
          "metadata": {},
          "execution_count": 97
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.stack([A,B],dim=0)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "k--e5X1Czm-G",
        "outputId": "1bd79fb5-3d9e-4d42-d058-8cf0fb8fb35d"
      },
      "execution_count": 98,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[10, 15, 20, 25, 30, 35, 40, 45],\n",
              "        [20, 25, 30, 35, 40, 45, 50, 55]])"
            ]
          },
          "metadata": {},
          "execution_count": 98
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.stack([A,B],dim=1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FD_SPrhqztHS",
        "outputId": "c46a6102-e100-4a8e-85c2-50c0f9e66de8"
      },
      "execution_count": 99,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[10, 20],\n",
              "        [15, 25],\n",
              "        [20, 30],\n",
              "        [25, 35],\n",
              "        [30, 40],\n",
              "        [35, 45],\n",
              "        [40, 50],\n",
              "        [45, 55]])"
            ]
          },
          "metadata": {},
          "execution_count": 99
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "torch.stack([A,B])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SvUdLxuVz2tU",
        "outputId": "59e389e0-d237-49f6-8e8d-9a5af34c28b6"
      },
      "execution_count": 100,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[10, 15, 20, 25, 30, 35, 40, 45],\n",
              "        [20, 25, 30, 35, 40, 45, 50, 55]])"
            ]
          },
          "metadata": {},
          "execution_count": 100
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# indexing of tensors\n",
        "y = torch.stack([A,B,A,B,A,B,A,B])\n",
        "y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UHrqmI3sz8fY",
        "outputId": "0156b689-c19d-4f19-f304-4559979ab6ee"
      },
      "execution_count": 104,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[10, 15, 20, 25, 30, 35, 40, 45],\n",
              "        [20, 25, 30, 35, 40, 45, 50, 55],\n",
              "        [10, 15, 20, 25, 30, 35, 40, 45],\n",
              "        [20, 25, 30, 35, 40, 45, 50, 55],\n",
              "        [10, 15, 20, 25, 30, 35, 40, 45],\n",
              "        [20, 25, 30, 35, 40, 45, 50, 55],\n",
              "        [10, 15, 20, 25, 30, 35, 40, 45],\n",
              "        [20, 25, 30, 35, 40, 45, 50, 55]])"
            ]
          },
          "metadata": {},
          "execution_count": 104
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Get all values of 0th dimension and the 1st index of 1st dimension\n",
        "y[:, 1]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "d1SdXt1D1BE-",
        "outputId": "00dc2db8-6016-4cb3-a8a6-d18acb86d19e"
      },
      "execution_count": 105,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([15, 25, 15, 25, 15, 25, 15, 25])"
            ]
          },
          "metadata": {},
          "execution_count": 105
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "D = torch.tensor([[[12,13,14],\n",
        "                   [15,16,17],\n",
        "                  [18,19,20]]])"
      ],
      "metadata": {
        "id": "-gfBvJne2KrX"
      },
      "execution_count": 109,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Get all values of 0th & 1st dimensions but only index 1 of 2nd dimension\n",
        "D[:, :, 1]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sSCv_nQL1E0l",
        "outputId": "fa3bf4e3-2ff2-4b6a-e241-7b60aa0364ae"
      },
      "execution_count": 111,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[13, 16, 19]])"
            ]
          },
          "metadata": {},
          "execution_count": 111
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Get all values of the 0 dimension but only the 1 index value of the 1st and 2nd dimension\n",
        "D[:, 1, 1]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-AidgtJY1XmO",
        "outputId": "3bfa3e22-b83a-497e-c751-7e0fc65a66d8"
      },
      "execution_count": 112,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([16])"
            ]
          },
          "metadata": {},
          "execution_count": 112
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Get index 0 of 0th and 1st dimension and all values of 2nd dimension \n",
        "D[0, 0, :] # same as D[0][0]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EcPobPYq2u6I",
        "outputId": "e408e56f-2646-4e93-a600-f555c2d84ab8"
      },
      "execution_count": 113,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([12, 13, 14])"
            ]
          },
          "metadata": {},
          "execution_count": 113
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "D[0][0]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yFy529YN20yU",
        "outputId": "ed713f60-4e3d-4241-a1e7-8c16de538ff7"
      },
      "execution_count": 114,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([12, 13, 14])"
            ]
          },
          "metadata": {},
          "execution_count": 114
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Check for GPU\n",
        "import torch\n",
        "torch.cuda.is_available()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "InLzhd6m23mD",
        "outputId": "5736a06a-e1c8-4823-cda4-94448a9e53b7"
      },
      "execution_count": 115,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 115
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Set device type\n",
        "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
        "device"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "KtpBh0Ct3Iza",
        "outputId": "44dd7881-49f1-4a8f-da4f-74ddb3bb9adb"
      },
      "execution_count": 116,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'cpu'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 116
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Count number of devices\n",
        "torch.cuda.device_count()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bpYfNDHW3QMp",
        "outputId": "ed2a6c1c-b1b3-4944-eeb8-18de491d32bc"
      },
      "execution_count": 117,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {},
          "execution_count": 117
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# x = torch.randn(2, 2, device='cpu') #on cpu\n",
        "# x = torch.randn(2, 2, device='gpu') #on gpu\n",
        "\n",
        "# x = torch.randn(2, 2, device=device) #platform agnostic"
      ],
      "metadata": {
        "id": "f28qusyk7lyz"
      },
      "execution_count": 132,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "kA1awrSJ7lio"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "98vOhj-n7lTR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# flatten tensor like numpy\n",
        "D.flatten()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XrpXEw5Q3VZj",
        "outputId": "f41365b4-9172-49da-d98f-9f51c32a7923"
      },
      "execution_count": 118,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([12, 13, 14, 15, 16, 17, 18, 19, 20])"
            ]
          },
          "metadata": {},
          "execution_count": 118
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Concatenate along rows\n",
        "cat_rows = torch.cat((A, B), dim=0)\n",
        "cat_rows"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Tm2RZ3vC4ytN",
        "outputId": "c2340b29-df29-4762-8886-63bdb244616e"
      },
      "execution_count": 125,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([10, 15, 20, 25, 30, 35, 40, 45, 20, 25, 30, 35, 40, 45, 50, 55])"
            ]
          },
          "metadata": {},
          "execution_count": 125
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "A.reshape(2,4)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fpEjBJXZ5Jis",
        "outputId": "2aa6da4b-6880-4c22-b6f0-bc25fc5a7f1f"
      },
      "execution_count": 129,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[10, 15, 20, 25],\n",
              "        [30, 35, 40, 45]])"
            ]
          },
          "metadata": {},
          "execution_count": 129
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "B.reshape(2,4)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cLS1nlzh5MLS",
        "outputId": "ca63d52b-f8bb-4bbf-91b7-b54b1fa2774d"
      },
      "execution_count": 130,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[20, 25, 30, 35],\n",
              "        [40, 45, 50, 55]])"
            ]
          },
          "metadata": {},
          "execution_count": 130
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "cat_cols = torch.cat((A.reshape(2,4), B.reshape(2,4)), dim=1)\n",
        "cat_cols"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PlapuS0u5NMA",
        "outputId": "386bf688-c991-4fda-a53a-3dc2969ebd36"
      },
      "execution_count": 131,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[10, 15, 20, 25, 20, 25, 30, 35],\n",
              "        [30, 35, 40, 45, 40, 45, 50, 55]])"
            ]
          },
          "metadata": {},
          "execution_count": 131
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "Xo8Thxhx5jxc"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.4"
    },
    "colab": {
      "name": "Torch_AI_1.ipynb",
      "provenance": [],
      "collapsed_sections": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}